package cn.tedu.servlet;

import cn.tedu.dao.IUser;
import cn.tedu.dao.impl.IUserImpl;
import cn.tedu.entity.User;
import cn.tedu.util.C3p0DBUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * <pre>
 *      实现User的相关控制类.
 * </pre>
 *
 * @author restyhap
 * @since 2019-11-04 14:32
 */
@WebServlet ("*.user")
public class UserServlet extends HttpServlet {
    @Override
    public void init(ServletConfig config) throws ServletException {
        System.out.println("UserServlet... init()... Servlet启动");
    }

    @Override
    public void destroy() {
        System.out.println("UserServlet... destroy()... Servlet销毁");
    }

    IUser iUser = null;


    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        iUser = new IUserImpl();
        String uriStr = req.getRequestURI();
        String action = uriStr.substring(uriStr.lastIndexOf("/")+1,uriStr.indexOf(".user"));
        System.out.println("UserServlet... 当前的请求是"+action);

        if("reg".equals(action)){

        //todo :忽略数据库,关注传参及页面获取参数的EL表达式.
        req.getServletContext().setAttribute("admin","applicationAdmin");
        req.getSession().setAttribute("admin","sessionAdmin");
        req.setAttribute("admin","requestAdmin");

        req.getRequestDispatcher("index.jsp").forward(req,resp);

        //当前Servlet是一个应该,需要转页面去实现效果的查看.目前不设置页面的访问域












//            reg(req,resp);
        }

    }

    private int reg(HttpServletRequest req,HttpServletResponse resp){
        int i = 2 ;
        Connection conn = C3p0DBUtil.getConnection();




//        User user = iUser.queryUser(req.getParameter("username"));
        System.out.println("查询的Conn是:"+conn);
        return i ;
    }


}
